package com.alkaid.novel.service.impl;

import com.alkaid.commons.entity.AnimePageEntity;
import com.alkaid.commons.entity.Novel;
import com.alkaid.commons.entity.NovelDetails;
import com.alkaid.commons.ex.DataNotFoundException;
import com.alkaid.novel.mapper.NovelDetailsMapper;
import com.alkaid.novel.mapper.NovelMapper;
import com.alkaid.novel.service.NovelService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.alkaid.commons.msg.DataTypeEnum.NOVEL;
import static com.alkaid.commons.msg.DataTypeEnum.NOVEL_DETAIL;

/**
 * @author Kylin
 */
@Service
public class NovelServiceImpl implements NovelService {

    private final NovelMapper novelMapper;
    private final NovelDetailsMapper novelDetailsMapper;

    public NovelServiceImpl(NovelMapper novelMapper, NovelDetailsMapper novelDetailsMapper) {
        this.novelMapper = novelMapper;
        this.novelDetailsMapper = novelDetailsMapper;
    }

    @Override
    public List<Novel> getHotList(Integer num) throws DataNotFoundException {
        LambdaQueryWrapper<Novel> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Novel::getId, Novel::getName, Novel::getHot).orderByDesc(Novel::getHot)
                .last("limit " + num);
        List<Novel> list = novelMapper.selectList(wrapper);
        if (list == null || list.size() == 0) {
            throw new DataNotFoundException();
        }
        return list;
    }

    @Override
    public List<Novel> getCountList(Integer num) throws DataNotFoundException {
        LambdaQueryWrapper<Novel> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Novel::getId, Novel::getName, Novel::getCover, Novel::getCount, Novel::getTag, Novel::getAllNum,
                Novel::getRefreshNum, Novel::getStatus).orderByDesc(Novel::getCount).last("limit " + num);
        List<Novel> list = novelMapper.selectList(wrapper);
        if (list == null || list.size() == 0) {
            throw new DataNotFoundException();
        }
        return list;
    }

    @Override
    public PageInfo<Novel> getAll(AnimePageEntity page) throws DataNotFoundException {
        PageHelper.startPage(page.getPage(), page.getRows(), page.getOrderBy());
        Page<Novel> list = novelMapper.findAll(page.getSql());
        return new PageInfo<>(list);
    }

    @Override
    public Novel getNovelWithDetailsByNovelId(Integer id) throws DataNotFoundException {
        Novel novel = novelMapper.selectById(id);
        if (novel == null) {
            throw new DataNotFoundException(NOVEL);
        }
        novel.setAuthor(novelDetailsMapper.findAuthors(id));
        List<NovelDetails> novelDetails = novelDetailsMapper.findByNovelId(id);
        if (novelDetails.size() == 0) {
            throw new DataNotFoundException(NOVEL_DETAIL);
        }
        novel.setNovelDetails(novelDetails);
        return novel;
    }
}
